class Person < ActiveRecord::Base
  belongs_to :church
  belongs_to :person_status, :foreign_key => "status_id"
  
  validates :first_name, :presence => true, :length => { :maximum => 255 }
  validates :last_name, :presence => true, :length => { :maximum => 255 }
  validates :address, :presence => true, :length => { :maximum => 255 }
  validates :city, :presence => true, :length => { :maximum => 255 }
  validates :state, :presence => true, :length => { :maximum => 2 }
  validates :zip_code, :presence => true, :zipcode => true, :length => { :maximum => 10 }
  validates :email, :email => true, :length => { :maximum => 255 }
  validates :status_id, :presence => true
  validates_uniqueness_of :last_name, :scope => [:church_id, :first_name, :address], :message => "name, address must be unique."
  
  paginates_per 20
  
  def full_name
    [last_name, first_name].join(', ')
  end
  
  def full_address
    [[address, city].join(', '), [state, zip_code]].join(' ')
  end
  
  def self.enabled_only
    where("enabled = true")
  end
  
  def self.disabled_only
    where("enabled = false")
  end
  
  def self.search(search)
    if search
      where('last_name LIKE ? OR first_name LIKE ? OR address LIKE ? OR email LIKE ?', "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
    else
      scoped
    end
  end
end
